"""
检索器 - 负责从第二阶段数据中检索和过滤相关内容
"""

from typing import Dict, List, Any, Optional, Tuple
import time
from loguru import logger

from .utils.data_loader import DataLoader, ProcessingElement
from .config import Stage3Config


class Retriever:
    """检索器类，负责数据检索和过滤"""
    
    def __init__(self, config: Stage3Config):
        self.config = config
        self.data_loader = DataLoader(config.storage_path)
        
    def retrieve_for_agent(self, agent_type: str, session_id: str = None) -> Dict[str, Any]:
        """为特定代理检索相关数据"""
        start_time = time.time()
        
        try:
            # 获取会话ID
            if not session_id:
                session_id = self.data_loader.get_latest_session_id()
            
            logger.info(f"为代理 {agent_type} 检索数据，会话ID: {session_id}")
            
            # 根据代理类型检索不同的数据
            if agent_type == "logic_coherence":
                return self._retrieve_for_logic_agent(session_id)
            elif agent_type == "compliance":
                return self._retrieve_for_compliance_agent(session_id)
            elif agent_type == "cad_review":
                return self._retrieve_for_cad_agent(session_id)
            elif agent_type == "table_data":
                return self._retrieve_for_table_agent(session_id)
            elif agent_type == "format_review":
                return self._retrieve_for_format_agent(session_id)
            elif agent_type == "summary":
                return self._retrieve_for_summary_agent(session_id)
            else:
                logger.warning(f"未知的代理类型: {agent_type}")
                return self._retrieve_all_data(session_id)
                
        except Exception as e:
            logger.error(f"数据检索失败: {e}")
            return {
                "success": False,
                "error": str(e),
                "data": [],
                "metadata": {}
            }
        finally:
            processing_time = time.time() - start_time
            logger.info(f"数据检索完成，耗时: {processing_time:.2f}秒")
    
    def _retrieve_for_logic_agent(self, session_id: str) -> Dict[str, Any]:
        """为逻辑连贯性代理检索数据"""
        # 主要检索文本内容，特别是描述性语句
        text_elements = self.data_loader.load_text_groups(session_id)
        
        # 过滤出描述性内容并计算质量分数
        descriptive_elements = []
        for element in text_elements:
            if self._is_descriptive_content(element):
                # 计算内容质量分数
                quality_score = self._calculate_content_quality(element)
                if isinstance(element, dict):
                    element['quality_score'] = quality_score
                else:
                    element.quality_score = quality_score
                descriptive_elements.append(element)
        
        # 按质量分数排序并限制为top15结果
        descriptive_elements = sorted(descriptive_elements, 
                                    key=lambda x: x.get('quality_score', 0) if isinstance(x, dict) else getattr(x, 'quality_score', 0), 
                                    reverse=True)[:15]
        
        # 获取相关的表格数据作为上下文（也限制为top15）
        table_elements = self.data_loader.load_tables(session_id)
        context_tables = []
        for elem in table_elements:
            if self._is_relevant_for_logic(elem):
                # 计算表格相关性分数
                relevance_score = self._calculate_table_relevance(elem)
                if isinstance(elem, dict):
                    elem['relevance_score'] = relevance_score
                else:
                    elem.relevance_score = relevance_score
                context_tables.append(elem)
        
        # 按相关性分数排序
        context_tables = sorted(context_tables, 
                              key=lambda x: x.get('relevance_score', 0) if isinstance(x, dict) else getattr(x, 'relevance_score', 0), 
                              reverse=True)[:15]
        
        # 提取location信息
        locations = self._extract_locations(descriptive_elements)
        
        return {
            "success": True,
            "data": descriptive_elements,
            "locations": locations,
            "context": {
                "tables": context_tables,
                "total_text_elements": len(text_elements),
                "filtered_elements": len(descriptive_elements)
            },
            "metadata": {
                "agent_type": "logic_coherence",
                "session_id": session_id,
                "retrieval_time": time.time()
            }
        }
    
    def _retrieve_for_compliance_agent(self, session_id: str) -> Dict[str, Any]:
        """为合规性代理检索数据"""
        # 检索所有文本内容，重点关注规范、法律相关内容
        text_elements = self.data_loader.load_text_groups(session_id)
        
        # 过滤出合规相关内容并计算合规相关性分数
        compliance_elements = []
        for element in text_elements:
            if self._is_compliance_related(element):
                # 计算合规相关性分数
                compliance_score = self._calculate_compliance_relevance(element)
                if isinstance(element, dict):
                    element['compliance_score'] = compliance_score
                else:
                    element.compliance_score = compliance_score
                compliance_elements.append(element)
        
        # 按合规相关性分数排序并限制为top15结果
        compliance_elements = sorted(compliance_elements, 
                                   key=lambda x: x.get('compliance_score', 0) if isinstance(x, dict) else getattr(x, 'compliance_score', 0), 
                                   reverse=True)[:15]
        
        # 获取相关的表格数据（也限制为top15）
        table_elements = self.data_loader.load_tables(session_id)
        compliance_tables = []
        for elem in table_elements:
            if self._is_compliance_table(elem):
                # 计算表格相关性分数
                relevance_score = self._calculate_table_relevance(elem)
                if isinstance(elem, dict):
                    elem['relevance_score'] = relevance_score
                else:
                    elem.relevance_score = relevance_score
                compliance_tables.append(elem)
        
        # 按相关性分数排序
        compliance_tables = sorted(compliance_tables, 
                                 key=lambda x: x.get('relevance_score', 0) if isinstance(x, dict) else getattr(x, 'relevance_score', 0), 
                                 reverse=True)[:15]
        
        # 加载规则文档作为检索上下文
        rules_context = self._load_rules_context()
        
        # 提取location信息
        locations = self._extract_locations(compliance_elements)
        
        return {
            "success": True,
            "data": compliance_elements,
            "locations": locations,
            "context": {
                "tables": compliance_tables,
                "rules": rules_context,
                "total_text_elements": len(text_elements),
                "filtered_elements": len(compliance_elements)
            },
            "metadata": {
                "agent_type": "compliance",
                "session_id": session_id,
                "retrieval_time": time.time()
            }
        }
    
    def _retrieve_for_cad_agent(self, session_id: str) -> Dict[str, Any]:
        """为CAD审查代理检索数据"""
        # 主要检索图像数据
        image_elements = self.data_loader.load_images(session_id)
        
        # 过滤出CAD图纸 - 不限制数量，所有图像都传给CAD专家
        cad_images = []
        for element in image_elements:
            if self._is_cad_image(element):
                cad_images.append(element)
        
        # 如果没有明确的CAD图像，则传递所有图像给CAD专家进行判断
        if not cad_images:
            cad_images = image_elements
        
        # 获取相关的文本描述作为上下文
        text_elements = self.data_loader.load_text_groups(session_id)
        cad_descriptions = [elem for elem in text_elements if self._is_cad_description(elem)]
        
        return {
            "success": True,
            "data": cad_images,
            "context": {
                "descriptions": cad_descriptions,
                "total_images": len(image_elements),
                "cad_images": len(cad_images)
            },
            "metadata": {
                "agent_type": "cad_review",
                "session_id": session_id,
                "retrieval_time": time.time()
            }
        }
    
    def _retrieve_for_table_agent(self, session_id: str) -> Dict[str, Any]:
        """为表格数据代理检索数据"""
        # 主要检索表格数据 - 不限制数量，所有表格都传给表格专家
        table_elements = self.data_loader.load_tables(session_id)
        
        # 获取相关的文本描述作为上下文
        text_elements = self.data_loader.load_text_groups(session_id)
        table_descriptions = [elem for elem in text_elements if self._is_table_description(elem)]
        
        return {
            "success": True,
            "data": table_elements,
            "context": {
                "descriptions": table_descriptions,
                "total_tables": len(table_elements)
            },
            "metadata": {
                "agent_type": "table_data",
                "session_id": session_id,
                "retrieval_time": time.time()
            }
        }
    
    def _retrieve_for_format_agent(self, session_id: str) -> Dict[str, Any]:
        """为格式规范代理检索数据"""
        # 检索所有文本内容进行格式检查
        text_elements = self.data_loader.load_text_groups(session_id)
        
        # 限制为top15结果以获取更多相关内容
        text_elements = text_elements[:15]
        
        # 获取原始数据作为格式参考（也限制为top15）
        raw_elements = self.data_loader.load_raw_data(session_id)
        raw_elements = (raw_elements if raw_elements else [])[:15]
        
        return {
            "success": True,
            "data": text_elements,
            "context": {
                "raw_data": raw_elements,
                "total_elements": len(text_elements)
            },
            "metadata": {
                "agent_type": "format_review",
                "session_id": session_id,
                "retrieval_time": time.time()
            }
        }
    
    def _retrieve_all_data(self, session_id: str) -> Dict[str, Any]:
        """检索所有数据"""
        text_elements = self.data_loader.load_text_groups(session_id)
        image_elements = self.data_loader.load_images(session_id)
        table_elements = self.data_loader.load_tables(session_id)
        raw_elements = self.data_loader.load_raw_data(session_id)
        
        return {
            "success": True,
            "data": text_elements + image_elements + table_elements,
            "context": {
                "raw_data": raw_elements,
                "text_count": len(text_elements),
                "image_count": len(image_elements),
                "table_count": len(table_elements)
            },
            "metadata": {
                "agent_type": "all",
                "session_id": session_id,
                "retrieval_time": time.time()
            }
        }
    
    def _is_descriptive_content(self, element) -> bool:
        """判断是否为描述性内容"""
        # 处理字典格式的元素
        if isinstance(element, dict):
            # 检查是否有elements字段（分组数据）
            if 'elements' in element:
                # 对分组中的每个元素进行检查
                for sub_element in element.get('elements', []):
                    if self._is_descriptive_content(sub_element):
                        return True
                return False
            
            # 处理单个元素
            content = element.get('content', '')
            category = element.get('category', '')
            subcategory = element.get('subcategory', '')
        else:
            # 处理ProcessingElement对象
            if not hasattr(element, 'content') or not element.content:
                return False
            content = element.content
            category = element.category if hasattr(element, 'category') else ""
            subcategory = element.subcategory if hasattr(element, 'subcategory') else ""
        
        if not content:
            return False
            
        content_lower = content.lower()
        category_lower = category.lower() if category else ""
        subcategory_lower = subcategory.lower() if subcategory else ""
        
        # 扩展描述性关键词，提高识别精确度
        descriptive_keywords = [
            "描述", "说明", "介绍", "概述", "分析", "评估", "论述",
            "阐述", "解释", "总结", "综述", "方案", "规划", "设计",
            "建设", "项目", "单位", "情况", "基本", "主营", "业务",
            "技术", "先进性", "创新", "能力", "实际", "用地", "产业",
            "计划", "布局", "配置", "安排", "目标", "原则", "策略",
            "措施", "方法", "步骤", "流程", "现状", "背景", "基础",
            "条件", "环境", "资源", "需求", "问题", "挑战", "机遇",
            "优势", "特点", "特色", "亮点", "概况", "建议"
        ]
        
        # 检查内容类型，优先选择段落和叙述性文本
        if 'paragraph' in subcategory_lower or 'narrative' in subcategory_lower or 'list_item' in subcategory_lower:
            return True
        
        # 检查内容长度，较长的内容更可能是描述性的
        if len(content) < 10:
            return False
        
        # 检查关键词密度
        keyword_count = 0
        for keyword in descriptive_keywords:
            if keyword in content_lower or keyword in category_lower or keyword in subcategory_lower:
                keyword_count += 1
        
        # 如果包含多个关键词，更可能是描述性内容
        if keyword_count >= 2 or (keyword_count >= 1 and len(content) > 50):
            return True
        
        # 检查是否包含分析性语言
        analysis_patterns = ["因此", "所以", "由于", "基于", "通过", "实现", "达到", "提高", "改善"]
        for pattern in analysis_patterns:
            if pattern in content_lower:
                return True
        
        return False
    
    def _is_compliance_related(self, element) -> bool:
        """判断是否为合规相关内容"""
        # 处理字典格式数据
        if isinstance(element, dict):
            # 如果是分组数据，检查其中的元素
            if 'elements' in element:
                for sub_element in element['elements']:
                    if self._is_compliance_related(sub_element):
                        return True
                return False
            
            # 单个元素
            content = element.get('content', '')
            category = element.get('category', '')
            subcategory = element.get('subcategory', '')
        else:
            # ProcessingElement对象
            if not element.content:
                return False
            content = element.content
            category = element.category or ""
            subcategory = element.subcategory if hasattr(element, 'subcategory') else ""
        
        if not content:
            return False
        
        content_lower = content.lower()
        category_lower = category.lower()
        subcategory_lower = subcategory.lower() if subcategory else ""
        
        # 扩展合规相关关键词，提高识别精确度
        compliance_keywords = [
            # 规划类
            "规划", "用地", "容积率", "建筑密度", "绿地率", "公共设施",
            # 建设类
            "建设", "施工", "工程", "建筑", "结构", "材料", "质量",
            # 环保类
            "环保", "环境", "污染", "排放", "噪音", "废水", "废气",
            # 安全类
            "安全", "消防", "防火", "疏散", "应急", "防护", "监控",
            # 法规类
            "标准", "规范", "法规", "条例", "要求", "规定", "办法",
            "许可", "审批", "备案", "验收", "监督", "检查", "评估",
            # 技术指标
            "指标", "参数", "限值", "标准值", "技术要求", "性能",
            # 程序类
            "程序", "流程", "手续", "申报", "公示", "听证", "评议",
            # 原有关键词
            "合规", "符合", "遵循", "执行", "实施", "监管",
            "证书", "资质", "土地", "报告", "审查", "批准", "申请"
        ]
        
        # 检查标题类内容，通常包含重要的合规信息
        if 'title' in subcategory_lower or 'title' in category_lower:
            for keyword in compliance_keywords:
                if keyword in content_lower:
                    return True
        
        # 检查内容长度和关键词密度
        if len(content) > 20:
            keyword_count = sum(1 for keyword in compliance_keywords if keyword in content_lower)
            # 如果包含多个关键词，更可能是合规相关内容
            if keyword_count >= 2 or (keyword_count >= 1 and len(content) > 100):
                return True
        
        # 单个关键词匹配（保持向后兼容）
        for keyword in compliance_keywords:
            if keyword in content_lower or keyword in category_lower:
                return True
        
        return False
    
    def _is_relevant_for_logic(self, element) -> bool:
        """判断表格是否与逻辑分析相关"""
        # 处理字典格式数据
        if isinstance(element, dict):
            # 如果是分组数据，检查其中的元素
            if 'elements' in element:
                for sub_element in element['elements']:
                    if self._is_relevant_for_logic(sub_element):
                        return True
                return False
            
            # 单个元素
            content = element.get('content', '')
        else:
            # ProcessingElement对象
            if not element.content:
                return False
            content = element.content
        
        if not content:
            return False
        
        content = content.lower()
        
        # 逻辑相关的表格类型
        logic_keywords = [
            "对比", "比较", "分析", "评估", "指标", "数据", "统计",
            "结果", "效果", "影响", "变化", "趋势"
        ]
        
        for keyword in logic_keywords:
            if keyword in content:
                return True
        
        return False
    
    def _is_compliance_table(self, element) -> bool:
        """判断表格是否与合规相关"""
        # 处理字典格式数据
        if isinstance(element, dict):
            # 如果是分组数据，检查其中的元素
            if 'elements' in element:
                for sub_element in element['elements']:
                    if self._is_compliance_table(sub_element):
                        return True
                return False
            
            # 单个元素
            content = element.get('content', '')
        else:
            # ProcessingElement对象
            if not element.content:
                return False
            content = element.content
        
        if not content:
            return False
        
        content = content.lower()
        
        # 合规相关的表格类型
        compliance_table_keywords = [
            "标准", "规范", "指标", "要求", "限值", "参数",
            "检测", "监测", "评价", "审查", "验收"
        ]
        
        for keyword in compliance_table_keywords:
            if keyword in content:
                return True
        
        return False
    
    def _is_cad_image(self, element) -> bool:
        """判断是否为CAD图纸"""
        # 处理字典格式数据
        if isinstance(element, dict):
            # 如果是分组数据，检查其中的元素
            if 'elements' in element:
                for sub_element in element['elements']:
                    if self._is_cad_image(sub_element):
                        return True
                return False
            
            # 单个元素
            source_type = element.get('source_type', '')
            metadata = element.get('metadata', {})
            content = element.get('content', '')
        else:
            # ProcessingElement对象
            source_type = element.source_type
            metadata = element.metadata or {}
            content = element.content or ""
        
        if source_type != 'image':
            return False
        
        # 检查文件名或描述
        filename = metadata.get('filename', '').lower()
        description = content.lower()
        
        # CAD图纸关键词
        cad_keywords = [
            "cad", "dwg", "图纸", "平面图", "立面图", "剖面图",
            "总平面", "建筑图", "结构图", "设备图", "管线图",
            "布置图", "详图", "节点图", "大样图", "图", "设计图",
            "施工图", "方案图", "效果图", "示意图", "位置图"
        ]
        
        for keyword in cad_keywords:
            if keyword in filename or keyword in description:
                return True
        
        return False
    
    def _is_cad_description(self, element) -> bool:
        """判断是否为CAD图纸描述"""
        # 处理字典格式数据
        if isinstance(element, dict):
            # 如果是分组数据，检查其中的元素
            if 'elements' in element:
                for sub_element in element['elements']:
                    if self._is_cad_description(sub_element):
                        return True
                return False
            
            # 单个元素
            content = element.get('content', '')
        else:
            # ProcessingElement对象
            if not element.content:
                return False
            content = element.content
        
        if not content:
            return False
        
        content = content.lower()
        
        # CAD描述关键词
        cad_desc_keywords = [
            "图纸", "图", "平面", "立面", "剖面", "详图",
            "布置", "设计", "尺寸", "标注", "比例"
        ]
        
        for keyword in cad_desc_keywords:
            if keyword in content:
                return True
        
        return False
    
    def _is_table_description(self, element) -> bool:
        """判断是否为表格描述"""
        # 处理字典格式数据
        if isinstance(element, dict):
            # 如果是分组数据，检查其中的元素
            if 'elements' in element:
                for sub_element in element['elements']:
                    if self._is_table_description(sub_element):
                        return True
                return False
            
            # 单个元素
            content = element.get('content', '')
        else:
            # ProcessingElement对象
            if not element.content:
                return False
            content = element.content
        
        if not content:
            return False
        
        content = content.lower()
        
        # 表格描述关键词
        table_desc_keywords = [
            "表", "统计", "数据", "指标", "参数", "清单",
            "列表", "汇总", "明细", "一览"
        ]
        
        for keyword in table_desc_keywords:
            if keyword in content:
                return True
        
        return False
    
    def _extract_locations(self, elements) -> str:
        """从检索到的元素中提取并聚合location信息"""
        locations = set()
        
        for element in elements:
            if isinstance(element, dict):
                # 检查是否有elements字段（分组数据）
                if 'elements' in element:
                    # 从分组的level3_number获取location
                    level3_number = element.get('level3_number', '')
                    if level3_number:
                        locations.add(level3_number)
                    
                    # 从分组中的每个元素获取location
                    for sub_element in element.get('elements', []):
                        sub_location = self._get_element_location(sub_element)
                        if sub_location:
                            locations.add(sub_location)
                else:
                    # 处理单个元素
                    location = self._get_element_location(element)
                    if location:
                        locations.add(location)
        
        # 将locations转换为排序的字符串
        if locations:
            sorted_locations = sorted(list(locations))
            return ', '.join(sorted_locations)
        else:
            return '未知位置'
    
    def _get_element_location(self, element) -> str:
        """从单个元素中获取location信息"""
        if not isinstance(element, dict):
            return ''
        
        # 优先从metadata中获取level3_number
        metadata = element.get('metadata', {})
        level3_number = metadata.get('level3_number', '')
        if level3_number:
            return level3_number
        
        # 如果metadata中没有，尝试从元素本身获取
        level3_number = element.get('level3_number', '')
        if level3_number:
            return level3_number
        
        # 如果都没有，尝试从parent_title中提取
        parent_title = metadata.get('parent_title', '')
        if parent_title:
            # 使用正则表达式提取章节号
            import re
            pattern = r'(\d+\.\d+\.\d+)'
            match = re.search(pattern, parent_title)
            if match:
                return match.group(1)
        
        return ''
    
    def _load_rules_context(self) -> Dict[str, Any]:
        """加载规则文档作为检索上下文"""
        try:
            # 这里可以加载外部规则文档
            # 暂时返回基本的规则上下文
            return {
                "rules_loaded": True,
                "rule_categories": [
                    "规划合规性", "建设规范", "环保要求", "安全标准",
                    "程序合规", "历史文化保护", "公共利益保障"
                ],
                "source": "内置规则库"
            }
        except Exception as e:
            logger.error(f"加载规则上下文失败: {e}")
            return {"rules_loaded": False, "error": str(e)}
    
    def _calculate_content_quality(self, element) -> float:
        """计算内容质量分数"""
        try:
            # 处理字典格式数据
            if isinstance(element, dict):
                if 'elements' in element:
                    # 对分组数据，计算所有子元素的平均质量分数
                    scores = []
                    for sub_element in element.get('elements', []):
                        scores.append(self._calculate_content_quality(sub_element))
                    return sum(scores) / len(scores) if scores else 0.0
                
                content = element.get('content', '')
                category = element.get('category', '')
                subcategory = element.get('subcategory', '')
            else:
                # ProcessingElement对象
                content = element.content or ''
                category = element.category or ''
                subcategory = element.subcategory if hasattr(element, 'subcategory') else ''
            
            if not content:
                return 0.0
            
            score = 0.0
            content_lower = content.lower()
            
            # 内容长度评分（适中长度得分更高）
            length = len(content)
            if 50 <= length <= 500:
                score += 2.0
            elif 20 <= length < 50 or 500 < length <= 1000:
                score += 1.0
            elif length > 1000:
                score += 0.5
            
            # 描述性关键词密度评分
            descriptive_keywords = [
                "描述", "说明", "介绍", "概述", "分析", "评估", "论述",
                "阐述", "解释", "总结", "综述", "方案", "规划", "设计"
            ]
            keyword_count = sum(1 for keyword in descriptive_keywords if keyword in content_lower)
            score += min(keyword_count * 0.5, 2.0)
            
            # 结构化内容评分
            if 'paragraph' in subcategory.lower() or 'narrative' in subcategory.lower():
                score += 1.5
            elif 'title' in subcategory.lower():
                score += 1.0
            
            # 逻辑连接词评分
            logic_words = ["因此", "所以", "由于", "基于", "通过", "实现", "达到"]
            logic_count = sum(1 for word in logic_words if word in content_lower)
            score += min(logic_count * 0.3, 1.0)
            
            return min(score, 10.0)  # 最高10分
            
        except Exception as e:
            logger.error(f"计算内容质量分数失败: {e}")
            return 0.0
    
    def _calculate_compliance_relevance(self, element) -> float:
        """计算合规相关性分数"""
        try:
            # 处理字典格式数据
            if isinstance(element, dict):
                if 'elements' in element:
                    # 对分组数据，计算所有子元素的平均相关性分数
                    scores = []
                    for sub_element in element.get('elements', []):
                        scores.append(self._calculate_compliance_relevance(sub_element))
                    return sum(scores) / len(scores) if scores else 0.0
                
                content = element.get('content', '')
                category = element.get('category', '')
                subcategory = element.get('subcategory', '')
            else:
                # ProcessingElement对象
                content = element.content or ''
                category = element.category or ''
                subcategory = element.subcategory if hasattr(element, 'subcategory') else ''
            
            if not content:
                return 0.0
            
            score = 0.0
            content_lower = content.lower()
            
            # 合规关键词权重评分
            high_priority_keywords = ["规划", "标准", "规范", "法规", "条例", "要求", "规定"]
            medium_priority_keywords = ["建设", "环保", "安全", "消防", "质量", "监管"]
            low_priority_keywords = ["合规", "符合", "遵循", "执行", "实施", "审查"]
            
            # 高优先级关键词
            high_count = sum(1 for keyword in high_priority_keywords if keyword in content_lower)
            score += high_count * 2.0
            
            # 中优先级关键词
            medium_count = sum(1 for keyword in medium_priority_keywords if keyword in content_lower)
            score += medium_count * 1.0
            
            # 低优先级关键词
            low_count = sum(1 for keyword in low_priority_keywords if keyword in content_lower)
            score += low_count * 0.5
            
            # 标题类内容加权
            if 'title' in subcategory.lower() or 'title' in category.lower():
                score *= 1.5
            
            # 内容长度调整
            length = len(content)
            if length > 100:
                score += 1.0
            elif length > 50:
                score += 0.5
            
            return min(score, 10.0)  # 最高10分
            
        except Exception as e:
            logger.error(f"计算合规相关性分数失败: {e}")
            return 0.0
    
    def _calculate_table_relevance(self, element) -> float:
        """计算表格相关性分数"""
        try:
            # 处理字典格式数据
            if isinstance(element, dict):
                if 'elements' in element:
                    # 对分组数据，计算所有子元素的平均相关性分数
                    scores = []
                    for sub_element in element.get('elements', []):
                        scores.append(self._calculate_table_relevance(sub_element))
                    return sum(scores) / len(scores) if scores else 0.0
                
                content = element.get('content', '')
            else:
                # ProcessingElement对象
                content = element.content or ''
            
            if not content:
                return 0.0
            
            score = 0.0
            content_lower = content.lower()
            
            # 表格相关关键词评分
            table_keywords = [
                "对比", "比较", "分析", "评估", "指标", "数据", "统计",
                "结果", "效果", "影响", "变化", "趋势", "标准", "规范",
                "限值", "参数", "检测", "监测", "评价", "审查", "验收"
            ]
            
            keyword_count = sum(1 for keyword in table_keywords if keyword in content_lower)
            score += keyword_count * 1.0
            
            # 数字和单位的存在
            import re
            numbers = re.findall(r'\d+', content)
            if len(numbers) > 0:
                score += min(len(numbers) * 0.2, 2.0)
            
            # 表格结构特征
            if '|' in content or '\t' in content:
                score += 1.0
            
            return min(score, 10.0)  # 最高10分
            
        except Exception as e:
            logger.error(f"计算表格相关性分数失败: {e}")
            return 0.0
    
    def get_session_summary(self, session_id: str = None) -> Dict[str, Any]:
        """获取会话数据摘要"""
        if not session_id:
            session_id = self.data_loader.get_latest_session_id()
        
        try:
            metadata = self.data_loader.load_session_metadata(session_id)
            
            # 统计各类型数据数量
            text_count = len(self.data_loader.load_text_groups(session_id))
            image_count = len(self.data_loader.load_images(session_id))
            table_count = len(self.data_loader.load_tables(session_id))
            
            return {
                "session_id": session_id,
                "metadata": metadata,
                "data_summary": {
                    "text_elements": text_count,
                    "image_elements": image_count,
                    "table_elements": table_count,
                    "total_elements": text_count + image_count + table_count
                },
                "retrieval_time": time.time()
            }
            
        except Exception as e:
            logger.error(f"获取会话摘要失败: {e}")
            return {
                "session_id": session_id,
                "error": str(e),
                "data_summary": {},
                "retrieval_time": time.time()
            }
    
    def _retrieve_for_summary_agent(self, session_id: str) -> Dict[str, Any]:
        """为总结代理检索数据"""
        try:
            # 总结代理不需要原始数据，只需要会话摘要信息
            session_summary = self.get_session_summary(session_id)
            
            return {
                "success": True,
                "data": [],  # 总结代理不需要具体数据
                "context": {
                    "session_summary": session_summary,
                    "note": "总结代理将接收其他专家代理的分析结果"
                },
                "metadata": {
                    "agent_type": "summary",
                    "session_id": session_id,
                    "retrieval_time": time.time()
                }
            }
            
        except Exception as e:
            logger.error(f"为总结代理检索数据失败: {e}")
            return {
                "success": False,
                "error": str(e),
                "data": [],
                "metadata": {
                    "agent_type": "summary",
                    "session_id": session_id,
                    "retrieval_time": time.time()
                }
            }